package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.RequestHttp;
import com.hermit.qmst.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/1/3 0003.
 */
@ContentView(R.layout.new_activity_verify_phone)
public class VerifyPhoneActivity extends Activity implements View.OnClickListener {
    @ViewInject(R.id.iv_back)
    ImageView mIvBack;
    @ViewInject(R.id.tv_title)
    TextView mTvTitle;
    @ViewInject(R.id.et_name)
    EditText mEtName;
    @ViewInject(R.id.et_code)
    EditText mEtCode;
    @ViewInject(R.id.bt_get_code)
    Button mBtGetCode;
    @ViewInject(R.id.bt_save)
    Button mBtSave;
    private String getCodeUrl = Common.mUrl + "szdh/randCode.php?phone=";
    private String code = "0";
    public static String phone = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mBtSave.setOnClickListener(this);
        mBtGetCode.setOnClickListener(this);
        mIvBack.setOnClickListener(this);
        mTvTitle.setText("验证手机号码");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_save:
                if (mEtCode.getText().toString().trim().equals(code)) {
                    Intent intent = new Intent();
                    if (LoginActivity.isRegister) {
                        intent.setClass(VerifyPhoneActivity.this, ForegetPwdActivity.class);
                    } else {
                        intent.setClass(VerifyPhoneActivity.this, UppasswordActivity.class);
                    }
                    startActivity(intent);
                } else {
                    Toast.makeText(VerifyPhoneActivity.this, "验证码输入有误", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bt_get_code:
                getcode();
//                starTimerTask();
                break;
        }
    }


    public Timer mTimer = new Timer();// 定时器
    private int count = 60;
    /**
     * 消息处理器的应用
     */
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    mBtGetCode.setText("剩" + count + "s后重试");
                    break;
                case 2:
                    mTimer.cancel();//
                    mTimer = null;
                    mBtGetCode.setText("获取验证码");
                    mBtGetCode.setBackgroundColor(Color.parseColor("#08B5C8"));
                    mBtGetCode.setEnabled(true);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public void starTimerTask() {
        count = 60;
        mBtGetCode.setEnabled(false);
        mBtGetCode.setBackgroundColor(Color.parseColor("#e3e3e3"));
        try {
            if (mTimer != null) {
                mTimer.cancel();// 退出之前的mTimer
                mTimer = null;
            }
            mTimer = new Timer();// new一个Timer,否则会报错

        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

        //创建定时线程执行更新任务
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                count--;
                if (count > 0) {

                    mHandler.sendEmptyMessage(1);// 向Handler发送消息
                } else {
                    mHandler.sendEmptyMessage(2);// 向Handler发送消息停止继续执行
                }

            }
        }, 1000, 1000);// 定时任务
    }

    private void getcode() {
        phone = mEtName.getText().toString().trim();
        if (!Utils.isMobileNO(phone)) {
            Toast.makeText(VerifyPhoneActivity.this, "请填入正确的手机号码", Toast.LENGTH_SHORT).show();
        } else {
            RequestHttp.getHttp(this, getCodeUrl + phone, new RequestHttp.HttpCallBack() {
                @Override
                public void successHttp(JSONObject jsonObject) {
                    try {
                        if (jsonObject.getBoolean("state")) {
                            starTimerTask();
                            code = jsonObject.getString("code");
                            Toast.makeText(VerifyPhoneActivity.this, "短信验证码获取成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(VerifyPhoneActivity.this, "短信验证码获取失败", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
